package com.lmf.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmf.eduservice.client.VodClient;
import com.lmf.eduservice.entity.EduCourse;
import com.lmf.eduservice.entity.vo.CourseInfoForm;
import com.lmf.eduservice.entity.vo.CoursePublishVo;
import com.lmf.eduservice.mapper.EduCourseMapper;
import com.lmf.eduservice.service.EduCourseService;
import com.lmf.handler.Exception.LmfException;
import com.lmf.resultCode.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lmf
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
@Api(description = "编辑课程的信息")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;
//    @Autowired
//    private EduCourseMapper eduCourseMapper;


    @ApiOperation("保存添加的课程相关信息")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfoForm) throws LmfException {
        String id = eduCourseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",id);
    }

    @ApiOperation("根据id,获取相应课程信息")
    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable("courseId") String courseId){
        CourseInfoForm courseInfoForm = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoForm",courseInfoForm);
    }

    @ApiOperation("根据id,修改相应课程信息")
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfoById(@RequestBody CourseInfoForm courseInfoForm) throws LmfException {
        eduCourseService.updateCourseInfo(courseInfoForm);
        return R.ok();
    }

    @ApiOperation("根据id,获取最终发布课程的所有信息")
    @GetMapping("/getCoursePublishVoById/{courseId}")
    public R getCoursePublishVoById(@PathVariable("courseId") String courseId){
//        CoursePublishVo publishCourseInfo = eduCourseMapper.getPublishCourseInfo(courseId);
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(courseId);
        return R.ok().data("publishCourseInfo",publishCourseInfo);
    }

    @ApiOperation("根据id,最终发布课程")
    @PostMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable("courseId") String courseId) throws LmfException {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");  //设置课程发布状态
        boolean b = eduCourseService.updateById(eduCourse);
        if (b) {
            return R.ok();
        }else {
            throw new LmfException(20001,"课程发布失败！");
        }
    }

    //TODO:需要完善分页查询
    @ApiOperation("获取所有的课程信息")
    @GetMapping("/getAllCourse")
    public R getAllCourse(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }

    @ApiOperation("根据id，删除课程")
    @DeleteMapping("/removeCourseById/{courseId}")
    public R removeCourseById(@PathVariable("courseId") String courseId) throws LmfException {
        boolean flag = eduCourseService.deleteCourseById(courseId);
        if (flag) {
            return R.ok();
        }else {
//            throw new LmfException(20001,"课程删除失败！");
            return R.error();
        }
    }


}

